<template>
  <div class="order-page">
    <div class="page-header">
      <div class="container">
        <h1 class="page-title">{{ tableId }}号桌 - 订单确认</h1>
        <p class="page-subtitle">请确认您的订单信息</p>
      </div>
    </div>

    <div class="container">
      <div class="order-content">
        <!-- 订单信息 -->
        <div class="order-info card">
          <h3>订单信息</h3>
          <div class="info-row">
            <span class="label">餐桌号：</span>
            <span class="value">{{ tableId }}号桌</span>
          </div>
          <div class="info-row">
            <span class="label">下单时间：</span>
            <span class="value">{{ orderTime }}</span>
          </div>
          <div class="info-row">
            <span class="label">订单状态：</span>
            <span class="value">
              <el-tag type="warning">待确认</el-tag>
            </span>
          </div>
        </div>

        <!-- 菜品清单 -->
        <div class="order-items card">
          <h3>菜品清单</h3>
          <div class="item-list">
            <div class="order-item" v-for="item in cartStore.items" :key="item.id">
              <div class="item-info">
                <span class="item-name">{{ item.name }}</span>
                <span class="item-quantity">×{{ item.quantity }}</span>
              </div>
              <span class="item-price">¥{{ (item.price * item.quantity).toFixed(2) }}</span>
            </div>
          </div>
        </div>

        <!-- 订单总计 -->
        <div class="order-total card">
          <div class="total-row">
            <span>商品总数：</span>
            <span>{{ cartStore.totalItems }} 件</span>
          </div>
          <div class="total-row">
            <span>商品总额：</span>
            <span>¥{{ cartStore.totalPrice.toFixed(2) }}</span>
          </div>
          <div class="total-row">
            <span>服务费：</span>
            <span>¥0.00</span>
          </div>
          <div class="total-row final">
            <span>实付金额：</span>
            <span class="final-price">¥{{ cartStore.totalPrice.toFixed(2) }}</span>
          </div>
        </div>

        <!-- 备注信息 -->
        <div class="order-notes card">
          <h3>备注信息</h3>
          <el-input
            v-model="notes"
            type="textarea"
            :rows="3"
            placeholder="请输入特殊要求或备注信息（可选）"
            maxlength="200"
            show-word-limit
          />
        </div>

        <!-- 操作按钮 -->
        <div class="order-actions">
          <el-button size="large" @click="goBack">
            <el-icon><ArrowLeft /></el-icon>
            返回修改
          </el-button>
          <el-button type="primary" size="large" @click="confirmOrder">
            <el-icon><Check /></el-icon>
            确认下单
          </el-button>
        </div>
      </div>
    </div>

    <!-- 下单成功弹窗 -->
    <el-dialog v-model="showSuccess" title="下单成功" width="90%" center>
      <div class="success-content">
        <el-icon size="80" color="#67c23a">
          <CircleCheckFilled />
        </el-icon>
        <h3>订单提交成功！</h3>
        <p>您的订单已提交，厨房正在准备中</p>
        <div class="order-number">
          订单号：{{ orderNumber }}
        </div>
      </div>
      <template #footer>
        <el-button type="primary" @click="goToMenu">继续点餐</el-button>
        <el-button @click="finishOrder">完成</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useCartStore } from '../stores/cart'
import { ElMessage, ElMessageBox } from 'element-plus'

const route = useRoute()
const router = useRouter()
const cartStore = useCartStore()

const tableId = computed(() => route.params.tableId)
const orderTime = ref('')
const notes = ref('')
const showSuccess = ref(false)
const orderNumber = ref('')

onMounted(() => {
  // 生成当前时间
  const now = new Date()
  orderTime.value = now.toLocaleString('zh-CN')
  
  // 生成订单号
  orderNumber.value = `ORDER${Date.now()}`
})

const goBack = () => {
  router.push(`/cart/${tableId.value}`)
}

const confirmOrder = async () => {
  try {
    await ElMessageBox.confirm(
      '确认提交订单？提交后将无法修改',
      '确认订单',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
    )
    
    // 模拟提交订单
    ElMessage.success('订单提交中...')
    setTimeout(() => {
      showSuccess.value = true
    }, 1000)
    
  } catch {
    // 用户取消
  }
}

const goToMenu = () => {
  showSuccess.value = false
  router.push(`/menu/${tableId.value}`)
}

const finishOrder = () => {
  showSuccess.value = false
  cartStore.clearCart()
  router.push('/')
}
</script>

<style scoped>
.order-page {
  min-height: 100vh;
  background: #f8f9fa;
}

.order-content {
  padding: 40px 0;
}

.card {
  background: white;
  border-radius: 16px;
  padding: 24px;
  margin-bottom: 24px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
}

.card h3 {
  margin: 0 0 20px 0;
  color: #333;
  font-size: 18px;
}

.info-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.info-row:last-child {
  border-bottom: none;
}

.label {
  color: #666;
}

.value {
  color: #333;
  font-weight: 500;
}

.item-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.order-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.order-item:last-child {
  border-bottom: none;
}

.item-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.item-name {
  color: #333;
  font-weight: 500;
}

.item-quantity {
  color: #666;
  font-size: 14px;
}

.item-price {
  color: #e6a23c;
  font-weight: bold;
}

.total-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.total-row:last-child {
  border-bottom: none;
}

.total-row.final {
  font-size: 18px;
  font-weight: bold;
  color: #333;
}

.final-price {
  color: #e6a23c;
  font-size: 24px;
}

.order-actions {
  display: flex;
  gap: 16px;
  justify-content: center;
  margin-top: 40px;
}

.success-content {
  text-align: center;
  padding: 40px 20px;
}

.success-content h3 {
  margin: 20px 0 10px;
  color: #333;
}

.success-content p {
  color: #666;
  margin-bottom: 20px;
}

.order-number {
  background: #f8f9fa;
  padding: 16px;
  border-radius: 8px;
  font-family: monospace;
  font-size: 16px;
  color: #333;
}

@media (max-width: 768px) {
  .order-actions {
    flex-direction: column;
  }
  
  .card {
    padding: 20px;
  }
}
</style> 